package br.com.yurikimo.ykkit;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class YKLayoutManager{
	private LayoutParams layoutParams = null;
	private RelativeLayout relativeLayout = null;
	private YKBaseComponent baseComponent = null;
	private YKContainer parent = null;
	private float x = 0;
	private float y = 0;
	private float width = 100;
	private float height = 50;
	
	public YKLayoutManager(YKBaseComponent baseComponent){
		this.baseComponent = baseComponent;
	}
	
	protected void setRelativeLayout(RelativeLayout relativeLayout){	
		this.relativeLayout = relativeLayout;
	}
	
	protected void addToLayout(RelativeLayout relativeLayout){
		this.relativeLayout = relativeLayout;
		
		relativeLayout.addView((View)baseComponent, layoutParams);
		relativeLayout.invalidate();		
	}
	
	protected RelativeLayout getLayoutView(Context context){
		if (relativeLayout == null){
			relativeLayout = new RelativeLayout(context);
			relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		}
		
		return relativeLayout;
	}
	
	protected void setBounds(float x, float y, float width, float height){
		layoutParams = null;
		
		layoutParams = new RelativeLayout.LayoutParams((int)width, (int)height);
		
		layoutParams.leftMargin = (int)x;
		layoutParams.topMargin = (int)y;		
		
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	protected void removeFromSuperView(){
		relativeLayout.removeView((View)baseComponent);
	}
	
	protected void refreshLayout(){
		setBounds(x, y, width, height);
		
		if (relativeLayout != null){
			if (relativeLayout.indexOfChild((View)baseComponent) >= 0){
				relativeLayout.removeView((View)baseComponent);
				
				relativeLayout.addView((View)baseComponent, layoutParams);
				relativeLayout.invalidate();
			}
		}
	}
	
	protected float getObjectWidth(){
		return width;
	}
	
	public float getObjectHeight(){
		return height;
	}
	
	public float getObjectX(){
		return x;
	}
	
	protected float getObjectY(){
		return y;
	}
	
	protected void setPosition(float x, float y){
		this.x = x;
		this.y = y;
	
		refreshLayout();
	}
	
	protected void setObjectWidth(float width){
		this.width = width;
	}
	
	protected void setObjectHeight(float height){
		this.height = height;
	}
	
	protected void setObjectParent(YKContainer parent){
		this.parent = parent;
	}
	
	protected YKContainer getObjectParent(){
		return parent;
	}
}
